package view.swing;

import cotroller.event.MovementListener;
import java.util.ArrayList;

/**
 *
 * @author Andrea
 */
public class GameCommands extends javax.swing.JPanel {

    /**
     * Creates new form GameCommands
     */
    public GameCommands() {
        listeners = new ArrayList<>(3);
        
        initComponents();
    }

    public boolean addMovementListener(MovementListener e) {
        return listeners.add(e);
    }

    public boolean removeMovementListener(MovementListener o) {
        return listeners.remove(o);
    }

    protected void clickLeft() {
        leftbtn.doClick();
    }
    
    protected void clickRight() {
        rightbtn.doClick();
    }
    
    protected void clickUp() {
        upbtn.doClick();
    }
    
    protected void clickDown() {
        downbtn.doClick();
    }
    
    protected void setEnabledButtons(boolean op){
        leftbtn.setEnabled(op);
        rightbtn.setEnabled(op);
        upbtn.setEnabled(op);
        downbtn.setEnabled(op);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        downbtn = new javax.swing.JButton();
        upbtn = new javax.swing.JButton();
        leftbtn = new javax.swing.JButton();
        rightbtn = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));

        setEnabled(false);
        setMinimumSize(new java.awt.Dimension(215, 110));

        downbtn.setText("Down");
        downbtn.setFocusable(false);
        downbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downbtnActionPerformed(evt);
            }
        });

        upbtn.setText("Up");
        upbtn.setFocusable(false);
        upbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upbtnActionPerformed(evt);
            }
        });

        leftbtn.setText("Left");
        leftbtn.setFocusable(false);
        leftbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leftbtnActionPerformed(evt);
            }
        });

        rightbtn.setText("Right");
        rightbtn.setFocusable(false);
        rightbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightbtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(leftbtn, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(filler1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(upbtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(downbtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rightbtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8, 8, 8))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(upbtn, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rightbtn, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                    .addComponent(filler1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(leftbtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(downbtn, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void upbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upbtnActionPerformed
        for (final MovementListener listener : listeners) {
            Thread a = new Thread() {
                @Override
                public void run() {
                    listener.moveUp();
                }
            };
            
            a.start();
        }
    }//GEN-LAST:event_upbtnActionPerformed

    private void leftbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leftbtnActionPerformed
        for (final MovementListener listener : listeners) {
            Thread a = new Thread() {
                @Override
                public void run() {
                    listener.moveLeft();
                }
            };
            
            a.start();
        }
    }//GEN-LAST:event_leftbtnActionPerformed

    private void rightbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightbtnActionPerformed
        for (final MovementListener listener : listeners) {
            Thread a = new Thread() {
                @Override
                public void run() {
                    listener.moveRight();
                }
            };
            
            a.start();
        }
    }//GEN-LAST:event_rightbtnActionPerformed

    private void downbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downbtnActionPerformed
        for (final MovementListener listener : listeners) {
            Thread a = new Thread() {
                @Override
                public void run() {
                    listener.moveDown();
                }
            };
            
            a.start();
        }
    }//GEN-LAST:event_downbtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton downbtn;
    private javax.swing.Box.Filler filler1;
    private javax.swing.JButton leftbtn;
    private javax.swing.JButton rightbtn;
    private javax.swing.JButton upbtn;
    // End of variables declaration//GEN-END:variables

    private final ArrayList<MovementListener> listeners;
}
